function Promise(executor){
    // 添加属性
    this.PromiseState = 'pending'
    this.PromiseResult = null
    this.callbacks = []
    const thia = this
    // resolve函数
    function resolve(data){
        if(thia.PromiseState !== 'pending') return
        // 1.修改对象的状态(promiseState)
        thia.PromiseState = 'fulfilled'
        // 2.设置对象的结果值(promiseResult)
        thia.PromiseResult = data
        setTimeout(()=>{
            thia.callbacks.forEach(item=>{
                item.onResolved(data)
            })
        })
    }
    // reject函数
    function reject(data){
        if(thia.PromiseState !== 'pending') return
        // 1.修改对象的状态(promiseState)
        thia.PromiseState = 'rejected'
        // 2.设置对象的结果值(promiseResult)
        thia.PromiseResult = data
        setTimeout(()=>{
            thia.callbacks.forEach(item=>{
                item.onReject(data)
            })
        })
    }
    // 处理异常
    try {
        executor(resolve, reject)
    }catch(e){
        reject(e)
    }
    // 同步调用执行器函数
    
}
Promise.prototype.then = function(onResolved,onReject){
    if(typeof onReject !== 'function'){

        onReject = reason =>{
            throw reason
        }
    }
    if(typeof onResolved !== 'function'){
        onResolved = value => value
    }
    return new Promise((resolve, reject)=>{
        this.callback = type=>{
            try {
                let result = type(this.PromiseResult)
                if(result instanceof Promise){
                    result.then(v=>{
                        resolve(v)
                    },r=>{
                        reject(r)
                    })
                }else{
                    resolve(result)
                }                
            } catch (error) {
                reject(error)
            }
        }
        if(this.PromiseState == 'fulfilled'){
            setTimeout(()=>{
                this.callback(onResolved)
            })
        }else if(this.PromiseState == 'rejected'){
            setTimeout(()=>{
                this.callback(onReject)
            })
        }else if(this.PromiseState == 'pending'){
            // 保存回调函数,处理异步方法，存进数组解决then多次调用队列问题
            this.callbacks = [...this.callbacks,{
                onResolved: ()=>{
                    this.callback(onResolved)
                },
                onReject: ()=>{
                    this.callback(onReject)
                }
            }]
        }
    })
}
Promise.prototype.catch = function(onReject){
    return this.then(undefined,onReject)
}
Promise.resolve = function(value){
    return new Promise((resolve,reject)=>{
        if(value instanceof Promise){
            value.then(v=>{
                resolve(v)
            },r=>{
                reject(r)
            })
        }else{
            resolve(value)
        }
    })
}
Promise.reject = function(reason){
    return new Promise((resolve,reject)=>{
        reject(reason)
    })
}
Promise.all = function(promises){
    return new Promise((resolve,reject)=>{
        let arr = []
        promises.forEach((promise,i)=>{
            promise.then(v=>{
                arr[i] = v
                console.log(i,promises.length-1)
                if(i == promises.length-1){
                    resolve(arr)
                }
            },r=>{
                reject()
            })
        })
        console.log(arr)
    })
}
Promise.race = function(promises){
    return new Promise((resolve,reject)=>{
        promises.forEach(item=>{
            item.then(v=>{
                resolve(v)
            },r=>{
                reject(r)
            })
        })
    })
}